home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 807 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  65 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Constructors and conversion operator
  5. Date: 21 Mar 1996 11:16:04 PST
  6. Organization: Sun Microsystems Inc.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4is5ao$dds@engnews1.Eng.Sun.COM>
  9. References: <31505061.2E03@cs.tu-berlin.de>
  10. Reply-To: clamage@Eng.Sun.COM
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 21 Mar 1996 17:57:44 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMVGrXUy4NqrwXLNJAQFBKwIAj2dR4J7Y/r3LlWZVD1Y4IhdX5a8o14ri
  15.     GU291fG+AZVgQE5S1W6tDEkmjfNj+ZstDX5I794cgn+JsVKhN7pYWw==
  16.     =FrWa
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article 2E03@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
  20. >Steve Clamage wrote:
  21. >>
  22. >> Suppose a copy constructor T::T(const T&) is viewed as a conversion
  23. >> from T to T. If such a conversion is "needed", the shortest conversion
  24. >> sequence must be chosen. That would be the null sequence, and so the
  25. >> copy ctor would never be used or even considered for that purpose.
  26. >> (Just as the sequence int->Rational->float would never be considered in
  27. >> converting an int to a float.)
  28. >
  29. >I'm not too sure here. First, if the copy constructor is the identity 
  30. >conversion, why is it called at all? I mean, if the identity conversion is 
  31. >eliminated from the conversion sequence, why isn't the call to the copy 
  32. >constructor itself eliminated? Second, if the copy constructor is called, why 
  33. >is it called only once? Consider
  34. >
  35. >class A { A(const A&); };
  36. >void foo(A);
  37. >A a;
  38. >
  39. >Now, foo(a) is implicitly converted to foo(A(a)). Why not to foo(A(A(a))? 
  40.  
  41. Let's not confuse type conversion with making a copy. In the call foo(a)
  42. no type conversion is required. Function foo requires an A object, and is
  43. invoked with an A object. But foo does require a copy of the actual argument,
  44. no matter what its origin. In this case foo gets a local copy of 'a'.
  45.  
  46. Example 2:
  47.     class B { public: operator A(); };
  48.     B b;
  49.     foo(b);
  50. We do need a conversion of 'b' to type A, which is available. A temporary A
  51. object is created by "B::operator A()", and a copy of that is passed to foo.
  52.  
  53. In both examples a copy of an A object is needed, accomplished as always
  54. by the copy constructor.
  55.  
  56. ---
  57. Steve Clamage, stephen.clamage@eng.sun.com
  58. ---
  59. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  60.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  61.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  62.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  63.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  64. ]
  65.